/*
 * 长文章增加“展开”和“收起”功能
 * 使用方法：页面引入此js，页面中包裹文章的元素加“class = 'cut_intro'”
 */

dom_ready(function()
{
	var oCon = getByClass(document, 'cut_intro');
	for(var i=0;i<oCon.length;i++)
	{
		var ifcut = subString(oCon[i].innerHTML,200,true);

		if(ifcut)
		{
			var total_con = document.createElement('pre');
			total_con.innerHTML = oCon[i].innerHTML;
			total_con.style.display = 'none';
      total_con.style.fontFamily = 'Microsoft YaHei';

			var some_con = document.createElement('span');
			some_con.innerHTML = subString(oCon[i].innerHTML,200,true);
			some_con.style.display = 'block';
      some_con.style.fontFamily = 'Microsoft YaHei';

			var open_con = document.createElement('a');
			open_con.innerHTML = '更多简介';
			open_con.href = "javascript:;";
			open_con.style.cssFloat="right";
			open_con.style.textDecoration="none";
			open_con.style.color="#c81623";
			open_con.style.paddingRight="100px";
			some_con.appendChild(open_con);

			var close_con = document.createElement('a');
			close_con.innerHTML = '收起';
			close_con.href = "javascript:;";
			close_con.style.cssFloat="right";
			close_con.style.textDecoration="none";
			close_con.style.color="#c81623";
			close_con.style.paddingRight="10px";
			total_con.appendChild(close_con);

			oCon[i].innerHTML = '';
			oCon[i].appendChild(total_con);
			oCon[i].appendChild(some_con);

			open_con.onclick = function()
			{
				this.parentNode.parentNode.children[0].style.display = 'block';
				this.parentNode.parentNode.children[1].style.display = 'none';
			}

			close_con.onclick = function()
			{
				this.parentNode.parentNode.children[1].style.display = 'block';
				this.parentNode.parentNode.children[0].style.display = 'none';
			}
		}
	}
})

function getByClass(oParent, sClass)
{
	if(oParent.getElementsByClassName)
	{
		return oParent.getElementsByClassName(sClass);
	}

	var aEle=oParent.getElementsByTagName('*');

	var re=new RegExp('\\b'+sClass+'\\b');
	var result=[];

	for(var i=0;i<aEle.length;i++)
	{
		if(re.test(aEle[i].className))
		{
			result.push(aEle[i]);
		}
	}

	return result;
}

function subString(str, len, hasDot)
{
    var newLength = 0;
    var newStr = "";
    var chineseRegex = /[^\x00-\xff]/g;
    var singleChar = "";
    var strLength = str.replace(chineseRegex,"**").length;
    for(var i = 0;i < strLength;i++)
    {
        singleChar = str.charAt(i).toString();
        if(singleChar.match(chineseRegex) != null)
        {
            newLength += 2;
        }
        else
        {
            newLength++;
        }
        if(newLength > len)
        {
            break;
        }
        newStr += singleChar;
    }

    if(strLength <= len)
    {
    	return false;
    }

    if(hasDot && strLength > len)
    {
        newStr += "...";
    }
    return newStr;
}

function dom_ready(fn){
	if(document.addEventListener){
		document.addEventListener('DOMContentLoaded',function(){
			fn && fn();
		},false);
	}else{
		document.onreadystatechange=function(){
			if(document.readyState=='complete'){
				fn && fn();
			}
		}
	}
}